package com.tf8.treasure.utils;

import org.bouncycastle.util.encoders.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;

/**
 * @program
 * @description AES128位CBC模式加解密
 **/
public class AESUtil {
    private final static String ENCODING = "UTF-8";

    private static String  iv = "3TUuPz3Eq5VaIN7Q1hABiQ==";
    private static  String aesKey = "22FyD6F753yQWAO6lf84zw==";

    public static String decode(String text) {
        return decode(aesKey,iv,Base64.decode(text));
    }

    public static String encode(String text) {
        byte[] encode = encode(aesKey, iv, text);
        return Base64.toBase64String(encode);
    }

    /**
     * @return java.lang.String
     * @description 生成base64 编码后的AES128位密钥
     * @date 2019/5/8 9:51
     */
    public static String getAESKey() {
        KeyGenerator kg = null;
        try {
            kg = KeyGenerator.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        kg.init(128);// 要生成多少位，只需要修改这里即可128, 192或256
        SecretKey sk = kg.generateKey();
        return Base64.toBase64String(sk.getEncoded());
    }

    /**
     * @param base64Key base64编码后的 AES key
     * @param text      待加密的字符串
     * @return byte[] 加密后的byte[] 数组
     * @description AES加密
     * @date 2019/5/8 9:51
     */
    public static byte[] encode(String base64Key, String base64KeyIv, String text) {
        if (text == null) {
            return null;
        }
        byte[] raw = Base64.decode(base64Key);
        byte[] iv = Base64.decode(base64KeyIv);

        byte[] encodeResult = null;
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            /**
             * aes-cbc模式加密在加密和解密是需要一个初始化向量(Initialization Vector, IV)，在每次加密之前或者解密之后，使用初始化向量与明文或密文异或。
             */
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv));
            encodeResult = cipher.doFinal(text.getBytes(Charset.forName(ENCODING)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encodeResult;
    }


    /**
     * @param base64Key base64编码后的 AES key
     * @param text      待解密的字符串
     * @return byte[] 解密后的byte[] 数组
     * @description AES解密
     * @date 2019/5/8 9:51
     */
    public static String decode(String base64Key, String base64KeyIv, byte[] text) {
        if (null == text) {
            return null;
        }
        byte[] raw = Base64.decode(base64Key);
        byte[] iv = Base64.decode(base64KeyIv);

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = null;
        byte[] original = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv));
            original = cipher.doFinal(text);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new String(original, Charset.forName(ENCODING));

    }
}
